<html>
<head><meta charset="utf-8"><title>miri question on raw pointer access for alignment · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html">miri question on raw pointer access for alignment</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="161887283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161887283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brian Campbell <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161887283">(Mar 27 2019 at 18:30)</a>:</h4>
<p>Given the recent release of miri via rustup, I figured I'd try it out on some popular crates to see if I found any issues.</p>
<p>Ran it on regex, and after making some changes to skip quickcheck, use non-randomized hashes, and avoid inline ASM and simd in memchr, ran into this. I'm not sure if this indicates actual UB, or just something that miri doesn't support; and if it's something that miri doesn't support, is there a supported way of doing what it's trying to do to find the first <code>USIZE</code> aligned pointer within the haystack:</p>
<div class="codehilite"><pre><span></span>error[E0080]: constant evaluation error: a raw memory access tried to access part of a pointer value as raw bytes
  --&gt; /home/lambda/.cargo/registry/src/github.com-1ecc6299db9ec823/memchr-2.2.0/src/fallback.rs:64:42
   |
64 |         ptr = ptr_add(ptr, USIZE_BYTES - (start_ptr as usize &amp; align));
   |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
   |
   = note: inside call to `memchr::fallback::memchr` at /home/lambda/.cargo/registry/src/github.com-1ecc6299db9ec823/memchr-2.2.0/src/lib.rs:153:9
   = note: inside call to `memchr::memchr::imp` at /home/lambda/.cargo/registry/src/github.com-1ecc6299db9ec823/memchr-2.2.0/src/lib.rs:159:9
</pre></div>



<a name="161908140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161908140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161908140">(Mar 27 2019 at 23:06)</a>:</h4>
<p>/cc <span class="user-mention" data-user-id="120791">@RalfJ</span> ^</p>



<a name="161908181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161908181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161908181">(Mar 27 2019 at 23:07)</a>:</h4>
<p>The text sounds like an actual error, but the code it points to (based on my own knowledge / guesses) seems off</p>



<a name="161908442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161908442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161908442">(Mar 27 2019 at 23:12)</a>:</h4>
<p>Like, the highlighted expression is not accessing as raw bytes. It may just be poorly worded for me though.</p>



<a name="161908535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161908535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161908535">(Mar 27 2019 at 23:14)</a>:</h4>
<p>Trying to make a related example:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span>: <span class="kt">u8</span> <span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a_ref</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">a</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a_ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a_ref</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a_aligned</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a_ptr</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">16</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">a_ptr</span><span class="p">.</span><span class="n">sub</span><span class="p">(</span><span class="n">a_aligned</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<div class="codehilite"><pre><span></span>error[E0080]: constant evaluation error: a raw memory access tried to access part of a pointer value as raw bytes
 --&gt; src/main.rs:5:21
  |
5 |     let a_aligned = a_ptr as usize % 16;
  |                     ^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
  |
  = note: inside call to `main` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:64:34
  = note: inside call to closure at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:52:53
  = note: inside call to closure at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panicking.rs:293:40
  = note: inside call to `std::panicking::try::do_call::&lt;[closure@DefId(1/1:1830 ~ std[82ff]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&amp;dyn std::ops::Fn() -&gt; i32 + std::marker::Sync + std::panic::RefUnwindSafe], i32&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panicking.rs:289:5
  = note: inside call to `std::panicking::try::&lt;i32, [closure@DefId(1/1:1830 ~ std[82ff]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&amp;dyn std::ops::Fn() -&gt; i32 + std::marker::Sync + std::panic::RefUnwindSafe]&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panic.rs:388:9
  = note: inside call to `std::panic::catch_unwind::&lt;[closure@DefId(1/1:1830 ~ std[82ff]::rt[0]::lang_start_internal[0]::{{closure}}[0]) 0:&amp;dyn std::ops::Fn() -&gt; i32 + std::marker::Sync + std::panic::RefUnwindSafe], i32&gt;` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:52:25
  = note: inside call to `std::rt::lang_start_internal` at /root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:64:5
  = note: inside call to `std::rt::lang_start::&lt;()&gt;`
</pre></div>



<a name="161908548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161908548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161908548">(Mar 27 2019 at 23:14)</a>:</h4>
<p>Now I'm curious, since I do the exact same thing in Jetscii (and for the same reason as well — to use the PCMPSTRx SIMD instrinsics)</p>



<a name="161908871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161908871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161908871">(Mar 27 2019 at 23:21)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> we gotta get Miri to give some <code>help: maybe you want to do X instead</code> :-)</p>



<a name="161931570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161931570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161931570">(Mar 28 2019 at 08:06)</a>:</h4>
<p>what you usually want to do is to use <a href="https://doc.rust-lang.org/std/primitive.slice.html#method.align_to" target="_blank" title="https://doc.rust-lang.org/std/primitive.slice.html#method.align_to">https://doc.rust-lang.org/std/primitive.slice.html#method.align_to</a> since that uses intrinsics internally to support all the use cases of miri. The low level operation for pointers is <a href="https://doc.rust-lang.org/std/primitive.pointer.html#method.align_offset" target="_blank" title="https://doc.rust-lang.org/std/primitive.pointer.html#method.align_offset">https://doc.rust-lang.org/std/primitive.pointer.html#method.align_offset</a></p>



<a name="161955247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161955247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161955247">(Mar 28 2019 at 14:05)</a>:</h4>
<p>yeah, this is because Miri doesnt pick an actual base address for an allocation</p>



<a name="161955278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161955278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161955278">(Mar 28 2019 at 14:05)</a>:</h4>
<p>so if the code does anything that depends on what the base address would be, you get an error</p>



<a name="161955316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161955316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161955316">(Mar 28 2019 at 14:05)</a>:</h4>
<p>we have a long-standing open issue to better classify errors into "UB found" and "hit engine limitation"...</p>



<a name="161974013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161974013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brian Campbell <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161974013">(Mar 28 2019 at 17:24)</a>:</h4>
<p>OK, so that would be an instance of <a href="https://github.com/rust-lang/miri/issues/417" target="_blank" title="https://github.com/rust-lang/miri/issues/417">https://github.com/rust-lang/miri/issues/417</a>, and it looks like the right way forward for memchr would be to switch to <code>slice::align_to</code>. Thanks for the info!</p>



<a name="161974197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161974197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brian Campbell <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161974197">(Mar 28 2019 at 17:26)</a>:</h4>
<p>A message about <code>maybe you want to do X instead</code> like the compiler gives would definitely be helpful, in addition to distinguishing UB from unsupported operations. Not sure if I'll have time to, but if I do have some cycles to look at contributing to miri, I might try to work on one of those.</p>



<a name="161974468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/161974468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#161974468">(Mar 28 2019 at 17:29)</a>:</h4>
<p>There's also <a href="https://github.com/rust-rfcs/const-eval/issues/4" target="_blank" title="https://github.com/rust-rfcs/const-eval/issues/4">https://github.com/rust-rfcs/const-eval/issues/4</a> tracking the enum split</p>



<a name="162125353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/miri%20question%20on%20raw%20pointer%20access%20for%20alignment/near/162125353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/miri.20question.20on.20raw.20pointer.20access.20for.20alignment.html#162125353">(Mar 30 2019 at 15:25)</a>:</h4>
<blockquote>
<p>This is a nightly-only experimental API.</p>
</blockquote>
<p>Well, that's kind of bunk, now innit.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>